import socket
import os

# 设置服务器的IP和端口
SERVER_IP = '10.250.5.103'
SERVER_PORT = 5001

# 发送文件

def send_file(filename):
    # 获取文件大小
    print("1")
    filesize = os.path.getsize(filename)

    # 建立socket连接
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect((SERVER_IP, SERVER_PORT))

    # 发送文件名和大小
    client_socket.send(f"{filename},{filesize}".encode('utf-8'))

    # 读取文件并发送
    with open(filename, 'rb') as f:
        bytes_sent = 0
        while (data := f.read(4096)):
            client_socket.sendall(data)
            bytes_sent += len(data)
            print(f'发送中... {bytes_sent / filesize * 100:.2f}%')

    print('文件发送完成')
    client_socket.close()

# 使用示例
if __name__ == '__main__':
    file_to_send = 'example.txt'  # 替换为要发送的文件路径
    send_file(file_to_send)
